import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { Toast } from 'vant'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: "/home"
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
  },
  {
    path: '/gameDetail',
    name: 'gameDetail',
    component: () => import('../views/GameDetail.vue')
  },
  {
    path: '/search',
    name: 'search',
    component: () => import('../views/SearchView.vue')
  },
  {
    path: '/periheral',
    name: 'periheral',
    component: () => import('../views/PeriheralView.vue')
  },
  {
    path: '/preferential',
    name: 'preferential',
    component: () => import('../views/preferentialView.vue')
  },
  {
    path: '/publishers',
    name: 'publishers',
    component: () => import('../views/publishersView.vue')
  },
  {
    path: '/userLogin',
    name: 'userLogin',
    component: () => import('../views/UserLogin.vue')
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('../views/registerView.vue')
  },
  {
    path: '/order',
    name: 'order',
    component: () => import('../views/orderView.vue')
  },
  {
    path: '/myOrder',
    name: 'myOrder',
    component: () => import('../views/myOrder.vue')
  },
  {
    path: '/headLine',
    name: 'headLine',
    component: () => import('../views/headLine.vue')
  },
  {
    path: '/headLineDetail',
    name: 'headLineDetail',
    component: () => import('../views/headLineDetail.vue'),
    beforeEnter: (to, from, next) => {
      let token = window.localStorage.getItem('token');
      if (!token) {
        Toast('请先登录再操作')
        next('/userLogin')
      } else {
        next()
      }
    }
  },
  {
    path: '/userInfo',
    name: 'userInfo',
    component: () => import('../views/userInfo.vue'),
    beforeEnter: (to, from, next) => {
      let token = window.localStorage.getItem('token');
      if (!token) {
        Toast('请先登录再操作')
        next('/userLogin')
      } else {
        next()
      }
    }
  },
  {
    path: '/shopCar',
    name: 'shopCar',
    component: () => import('../views/shopCar.vue'),
    beforeEnter: (to, from, next) => {
      let token = window.localStorage.getItem('token');
      if (!token) {
        Toast('请先登录再操作')
        next('/userLogin')
      } else {
        next()
      }
    }
  },
]


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  }
})

export default router
